package me.pcqpcq.android.data;

import java.io.File;
import java.text.DecimalFormat;

import android.os.StatFs;

/**
 * <p>数据类</p>
 * <p>计算容量</p>
 * @author pcqpcq
 *
 */
public class PathStorageData {
	
	private String externalCardPath;
	
	/**
	 * 计算外置卡容量的构造方法
	 * @param externalCardPath 外置卡路径
	 */
	public PathStorageData(String externalCardPath) {
		super();
		this.externalCardPath = externalCardPath;
	}
	
	/**
	 * 取得目录容量，以字节为单位
	 * @return 容量
	 */
	public static long getPathStorage(File pathname) {
		long pathSize = 0;
		if (pathname.isDirectory()) {
			if (pathname.canRead()) {
				File[] pathFiles = pathname.listFiles();
				if (pathFiles.length != 0) {
					for (int i = 0; i < pathFiles.length; i++) {
						pathSize += getPathStorage(pathFiles[i]);
					}
				}
			}
		} else if (pathname.length() != 0)
			pathSize += pathname.length();

		return pathSize;
	}
	
	/**
	 * 获取外置卡的总容量，单位为字节
	 * @param filePath
	 * @return total storage of SDcard
	 */
	public long getExternalStorage() {
		StatFs statFs = new StatFs(externalCardPath);
		long blockSize = statFs.getBlockSize(); // 取得Block的Size
		long blockCount = statFs.getBlockCount(); // 取得Block的数量
		return blockSize * blockCount;
	}
	
	/**
	 * 获取外置卡的剩余容量，单位为字节
	 * @param filePath
	 * @return available storage of SDcard
	 */
	public long getAvailableStorage() {
		StatFs statFs = new StatFs(externalCardPath);
		long blockSize = statFs.getBlockSize(); // 取得Block的Size
		long availableBlock = statFs.getAvailableBlocks(); // 可使用的Block数量
		return availableBlock * blockSize;
	}

	/**
	 * 转换字节容量为自然语言，单位为GB
	 * @param storage 字节容量
	 * @return 容量
	 */
	public double getHumanStorage(long storage) {
		// 转换数值
		double humanStorage = ((double) storage) / 1024 / 1024 / 1024;
		DecimalFormat df = new DecimalFormat("0.00");
		return Double.parseDouble(df.format(humanStorage));
	}
	
}
